package com.service.impl;

import com.address.Book;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bean.Goods;
import com.dao.GoodsMapper;
import com.dao.UserMapper;
import com.service.IGoodsService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author WxrStart
 * @create 2022-06-14 14:39
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper,Goods> implements IGoodsService{

    @Autowired
    public GoodsMapper goodsMapper;
    /**
     * 根据商品名称获取商品记录
     * @param goodsName
     * @return
     */
    @Override
    public Goods getGoodsByName(String goodsName) {
        QueryWrapper<Goods> queryWrapper=new QueryWrapper();
        queryWrapper.eq("goods_name",goodsName);
        return goodsMapper.selectOne(queryWrapper);
    }

    /**
     *  增加商品
     * @param goods
     * @return
     */
    @Override
    public Integer addGoods(Goods goods) {
        return goodsMapper.insert(goods);
    }

    @Override
    public Integer updateGoods(Goods goods) {
        return goodsMapper.updateById(goods);
    }

    /**
     * 根据商品名删除商品
     * @param goodsName
     * @return
     */
    @Override
    public Integer delGoods(String goodsName) {
        QueryWrapper<Goods> queryWrapper=new QueryWrapper();
        queryWrapper.eq("goods_name",goodsName);
        return goodsMapper.delete(queryWrapper);
    }

    /**
     * 查找哪一个商品是秒杀的商品
     * @return
     */
    @Override
    public Goods getGoodsByIs_Seckill() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("is_seckill",1);
        return goodsMapper.selectOne(queryWrapper);
    }

    /**
     * 通过orderId查找商品是否是当前商品)
     * @param orderId
     * @return
     */
    @Override
    public Goods getGoodsByOrderId(Integer orderId) {
        return goodsMapper.getGoodsByOrderId(orderId);
    }

    /**
     * 修改或添加商品
     * @param goods
     * @return
     */
    public Integer saveOrUpdateGoods(Goods goods){
        if (goods.getGoodsId()==null){
            //没有传入goodId则说明是需要新增商品
            goodsMapper.insert(goods);
            return 1;
        }else {
            //否则为更新当前的goods
            goodsMapper.updateGoodsDynamic(goods);
            return 2;
        }
    }

    /**
     * 根据商品id删除商品
     */
    public Integer deleteGoodsById(Integer id){
        int result = goodsMapper.deleteById(id);
        return result;
    }


    @Override
    public IPage<Goods> getPage(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        goodsMapper.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<Goods> getPage(int currentPage, int pageSize, Goods goods) {
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<Goods>();
        //lqw.like(Strings.isNotEmpty(goods.getType()),Book::getType,goods.getType());
        lqw.like(Strings.isNotEmpty(goods.getGoodsName()),Goods::getGoodsName,goods.getGoodsName());
        lqw.like(Strings.isNotEmpty(goods.getGoodsDisc()),Goods::getGoodsDisc,goods.getGoodsDisc());
        IPage page = new Page(currentPage,pageSize);
        goodsMapper.selectPage(page,lqw);
        return page;
    }

    @Override
    public List<Goods> getGoodsList(){
        return goodsMapper.getGoodsList();
    }
}
